package pe.org.ecampos.ecamposClientRest;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamReader;

import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.mapped.Configuration;
import org.codehaus.jettison.mapped.MappedNamespaceConvention;
import org.codehaus.jettison.mapped.MappedXMLStreamReader;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;

/**
 * Hello world!
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class App {
	
	
	 @Autowired
     private RestTemplate restTemplate;
	 
	 @Test
	 public void pruebaListaGson(){		 
		 String response = restTemplate.getForObject( "http://localhost:8080/restEcampos/rs/rest/vuelos/seguimiento2", String.class );
		Gson gson = new Gson();
		Type collectionType= new com.google.gson.reflect.TypeToken<List<Seguimiento>>(){}.getType();
        List<Seguimiento> listaPedidos= gson.fromJson(response	, collectionType);
       System.out.println( listaPedidos.size() );
       for(Seguimiento s: listaPedidos){
    	   System.out.println( s.toString());
       }
		 Assert.assertNotNull(listaPedidos);
	 }

	
	 public void pruebaLista(){		 
		 String response = restTemplate.getForObject( "http://localhost:8080/restEcampos/rs/rest/vuelos/seguimiento", String.class );
			 try {
            
             JAXBContext jc = JAXBContext.newInstance(Seguimiento.class);
		              
		     JSONObject obj = new JSONObject(response);
		     Configuration config = new Configuration();
		     MappedNamespaceConvention con = new MappedNamespaceConvention(config);
		     XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
		
		     System.out.println( " JSON = " + obj );
		     Unmarshaller unmarshaller = jc.createUnmarshaller();
		     Collection<Seguimiento> de = ( Collection<Seguimiento> ) unmarshaller.unmarshal(xmlStreamReader);
		     System.out.println(de.toString());
	     	            
		     } catch (Exception e) {
		             e.printStackTrace();
		     }     
		      
		     Assert.assertNotNull(response);
	 }
	 
	 @Test
	 public void pruebaElemento(){		 
		 String response = restTemplate.getForObject( "http://localhost:8080/restEcampos/rs/rest/vuelos/seguimiento/1", String.class );
		 try {
             
             JAXBContext jc = JAXBContext.newInstance(Seguimiento.class);
		              
		     JSONObject obj = new JSONObject(response);
		     Configuration config = new Configuration();
		     MappedNamespaceConvention con = new MappedNamespaceConvention(config);
		     XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
		
		     Unmarshaller unmarshaller = jc.createUnmarshaller();
		     Seguimiento de = (Seguimiento) unmarshaller.unmarshal(xmlStreamReader);
		     System.out.println(de.toString());
		  
		     } catch (Exception e) {
		             e.printStackTrace();
		     }               
		      
		     Assert.assertNotNull(response);
	 }
}
